import { IHelper  } from 'egg';

export default {
    addOne(v) {
        return v + 1
    },
    async createData(data,model){
    //    保存数据
        let result: boolean;
        let resultMsg: string;
        let createResult;
        let {_id, ...dataWithoutId} = data;
        createResult=await model.create(dataWithoutId).catch(err=>{
            console.error('保存err:',err);
        })
        result = createResult ? true : false;
        resultMsg = createResult ? '更新成功' : '更新失败';
        return {result,resultMsg};
    },

    async updateByQuery(this: IHelper, query, data, model): Promise<any> {
        //根据查询条件更新数据
        // const {ctx}:any = this;
        let result:boolean;
        let resultMsg:string;
        let updateResult;
        updateResult = await model.updateOne(query, data, {strict: true}).exec().catch(err=>{
            console.error('更新err:',err);
        });
        result = updateResult.nModified > 0 ? true : false
        resultMsg=updateResult.nModified > 0 ?'更新成功':'更新失败'
        return {result,resultMsg};
    },

    async updateById(this:IHelper,id, data,model):Promise<any> {
        //根据id查询数据
        let result:boolean;
        let resultMsg:string;
        let updateResult;
        updateResult = await model.findByIdAndUpdate(id, data).exec().catch(err=>{
            console.error('更新err:',err);
        });
        result = updateResult ? true : false;
        resultMsg = updateResult ? '更新成功' : '更新失败';
        return {result,resultMsg};
    }
}